package com.wlyuan.open.scanner.domain.goods;

import com.wlyuan.open.scanner.dao.entity.GoodsDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class GoodsConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "goodsId"),
                @Mapping(target = "barcode", source = "goodsBarcode"),
                @Mapping(target = "boxInput", source = "goodsAmount"),
                @Mapping(target = "grossWeight", source = "goodsMweight"),
                @Mapping(target = "height", source = "goodsHeight"),
                @Mapping(target = "length", source = "goodsLong"),
                @Mapping(target = "name", source = "goodsName"),
                @Mapping(target = "enName", source = "goodsEnname"),
                @Mapping(target = "netWeight", source = "goodsWeight"),
                @Mapping(target = "spec", source = "goodsSpc"),
                //老系统类型id
                @Mapping(target = "goodsTypeId", source = "goodsTypeid"),
                //老系统单位id
                @Mapping(target = "goodsUnitId", source = "goodsUnit"),
                @Mapping(target = "value", source = "goodsPrice"),
                @Mapping(target = "volume", source = "goodsVolume"),
                @Mapping(target = "width", source = "goodsWidth"),
                @Mapping(target = "hots", source = "goodsHotcount"),
                @Mapping(target = "customerId", source = "goodsCustomerid"),

                @Mapping(target = "tenantId", source = "goodsCreatorcompanyid"),
                @Mapping(target = "sourceKey", source = "goodsId"),
                @Mapping(target = "createTime", source = "goodsInserttime"),
                @Mapping(target = "updateTime", source = "goodsUpdatetime"),
                @Mapping(target = "createBy", source = "goodsCreator"),
                @Mapping(target = "updateBy", source = "goodsUpdater"),
        })
        @BaseMapping
        Goods convert(GoodsDO goodsDO);

        @AfterMapping
        default void after(@MappingTarget Goods target, GoodsDO source) {
            target.setSourceId(source.getGoodsId() + "#" + source.getGoodsCustomerid() + "#" + source.getCustomerCompanyId());
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Goods> convert(List<GoodsDO> list);

    }

    public static List<Goods> convert(List<GoodsDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Goods convert(GoodsDO creation) {
        return CONVERT.convert(creation);
    }

}
